package com.LeetCode.StackAndHeap;

import org.junit.Test;

import java.util.PriorityQueue;

/**
 * 数组中第 k 大的元素
 */
public class theKLargestNum {

    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> heap = new PriorityQueue<>(k);
        for(int num : nums){
            if(heap.size()<k){
                heap.offer(num);
            }else{
                if(heap.peek() < num){
                    heap.poll();
                    heap.offer(num);
                }
            }
        }
        return heap.peek();
    }

    @Test
    public void test(){
        int[] arr = {3,2,3,1,2,4,5,5,6};
        System.out.println(findKthLargest(arr,4));
    }
}
